//
//  UserTennisLineVC.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/29.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserTennisLineVC.h"
#import "UserGroup.h"
#import "UserArrowItem.h"
#import "UserCenterCell.h"
#import "UserTennisLineController.h"
#import "UserSizeAndTensionVC.h"
#import "UserSericesChildVC.h"
#import "UserSaveItem.h"

NSString *const userChangeUserTennisLineEquipNotification = @"userChangeUserTennisLineEquipNotification";

@interface UserTennisLineVC ()



@end

@implementation UserTennisLineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    // 添加通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeUserEquipsTennisLineNotification:) name:userChangeUserTennisLineEquipNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    if (self.selectCallback) {
        self.selectCallback(self.brandStr, self.modelStr, self.sizeStr,self.tensionStr);
    }
}

- (void)changeUserEquipsTennisLineNotification:(NSNotification *)note {
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    
    item.subTitle = [NSString stringWithFormat:@"%@\n%@", note.userInfo[@"Brand"], note.userInfo[@"Model"]];
    self.brandStr = note.userInfo[@"Brand"];
    self.modelStr = note.userInfo[@"Model"];

    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)loadData {
    __weak typeof(self) weakSelf = self;
    //    第一组
    UserGroup *group0 = [[UserGroup alloc] init];
    group0.headHeight = kAutoHei(24);
    
    /*"Brand and Model" = "品牌类型";
     "Gauge" = "规格";
     "Tension" = "张力";*/
    
    UserArrowItem *item0_0 = [UserArrowItem itemWithIcon:nil andTitle:NSLocalizedString(@"Brand and Model", nil) andSubTitle:[NSString stringWithFormat:@"%@\n%@", self.brandStr, self.modelStr] andTarget:[UserTennisLineController class]];
    
    // 如果连接的是海德的设备
    if ([User getLastUser].userDevice.oemType == OemTypeHD){
        item0_0 = [UserArrowItem itemWithIcon:nil andTitle:NSLocalizedString(@"Brand and Model", nil) andSubTitle:[NSString stringWithFormat:@"%@\n%@", self.brandStr, self.modelStr] andTarget:nil];
        
        NSArray *brandAndModelArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TennisLineBrandAndModel.plist" ofType:nil]];
        
        NSDictionary *obj = brandAndModelArray.firstObject;
        
        [item0_0 setBlock:^{
            UserSericesChildVC *childVc = [[UserSericesChildVC alloc] init];
            childVc.childType = SericesChildTennisLineBrandAndModle;
            childVc.content = obj[@"TennisLineBrand"];
            [childVc.groups addObject:[weakSelf racketModelArray:obj[@"TennisLineModel"]]];
            [weakSelf.navigationController pushViewController:childVc animated:YES];
        }];
    }
    
    UserArrowItem *item0_1 = [UserArrowItem itemWithTitle:NSLocalizedString(@"Gauge", nil) andSubTitle:self.sizeStr];
    
    __weak typeof(item0_1) weakItem0_1 = item0_1;
    
    [item0_1 setBlock:^{
        UserSizeAndTensionVC *sizeVc = [[UserSizeAndTensionVC alloc] init];
        sizeVc.title = weakItem0_1.title;
        sizeVc.contentValue = weakItem0_1.subTitle;
        [sizeVc setSelectCallback:^(NSString *callBackString) {
            weakItem0_1.subTitle = callBackString;
            weakSelf.sizeStr = callBackString;
            [weakSelf.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
        }];
        [weakSelf.navigationController pushViewController:sizeVc animated:YES];
        
    }];
    
    UserArrowItem *item0_2 = [UserArrowItem itemWithTitle:NSLocalizedString(@"Tension", nil) andSubTitle:self.tensionStr];
    __weak typeof(item0_2) weakItem0_2 = item0_2;
    [item0_2 setBlock:^{
        UserSizeAndTensionVC *tensionVc = [[UserSizeAndTensionVC alloc] init];
        tensionVc.title = weakItem0_2.title;
        tensionVc.contentValue = weakItem0_2.subTitle;
        [tensionVc setSelectCallback:^(NSString *callBackString) {
            weakItem0_2.subTitle = callBackString;
            weakSelf.tensionStr = callBackString;
            [weakSelf.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
        }];
        [weakSelf.navigationController pushViewController:tensionVc animated:YES];
        
    }];
    
    [group0.items addObjectsFromArray:@[item0_0, item0_1, item0_2]];
    [self.groups addObject:group0];
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserCenterCell *cell = [UserCenterCell settingViewCellTableView:tableView withStyle:UITableViewCellStyleValue1];
    
    UserGroup *group = self.groups[indexPath.section];
    UserItem *item = group.items[indexPath.row];
    cell.item = item;
    if (indexPath.row == group.items.count - 1) {
        [cell showLine:NO];
    }else {
        [cell showLine:YES];
    }
    if (indexPath.row == 0) {
        [cell showSectionSepTopLineView:YES];
    }else {
        [cell showSectionSepTopLineView:NO];
    }
    return cell;
}

- (UserGroup *)racketModelArray:(NSArray *)stringArr {
    
    UserGroup *group0 = [[UserGroup alloc] init];
    
    group0.headHeight = kAutoHei(24);
    
    [stringArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UserSaveItem *saveItem = [UserSaveItem itemWithTitle:obj];
        [group0.items addObject:saveItem];
    }];
    
    return group0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 64;
    }else {
        return 44;
    }
}

@end
